package io.github.l86400.util.bean;

import java.util.Collection;
import java.util.Map;

/**
 * @author 86400
 * @since  2021/12/15 9:06
 */
public class EmptyUtil {
    private EmptyUtil() {
    }

    /**
     * 判断对象是否为空 复合接口，当前支持
     * {@link #isEmpty(Object[])}
     * {@link #isEmpty(Map)}
     * {@link #isEmpty(CharSequence)}
     * {@link #isEmpty(Collection)}
     *
     * @param o Object
     * @return 为空{@code true} 不为空{@code false}
     */
    public static boolean isEmpty(final Object o) {
        if (null == o) {
            return true;
        } else if (o instanceof CharSequence) {
            CharSequence temp = (CharSequence) o;
            return isEmpty(temp);
        } else if (o instanceof Collection) {
            Collection temp = (Collection) o;
            return isEmpty(temp);
        } else if (o instanceof Map) {
            Map temp = (Map) o;
            return isEmpty(temp);
        } else if (o instanceof Object[]) {
            Object[] temp = (Object[]) o;
            return isEmpty(temp);
        } else {
            return false;
        }
    }

    /**
     * 字符串是否为空 null 或者 ""
     *
     * @param cs 字符串
     * @return 为空{@code true} 不为空{@code false}
     */
    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    /**
     * 集合是否为空
     *
     * @param collection 集合
     * @return 为空{@code true} 不为空{@code false}
     */
    public static boolean isEmpty(Collection collection) {
        return collection == null || collection.isEmpty();
    }

    /**
     * map是否为空
     *
     * @param map k-v
     * @return 为空{@code true} 不为空{@code false}
     */
    public static boolean isEmpty(Map map) {
        return map == null || map.isEmpty();
    }

    /**
     * 是否有空对象
     *
     * @param objects 数组
     * @return 有空{@code true} 全不为空{@code false}
     */
    public static boolean hasEmpty(Object... objects) {
        if (null == objects || objects.length == 0) {
            return true;
        }
        for (int i = 0; i < objects.length; i++) {
            if (isEmpty(objects[i])) {
                return true;
            }
        }
        return false;
    }

    /**
     * 数组是否为空
     *
     * @param objs 数组
     * @return 为空{@code true} 不为空{@code false}
     */
    public static boolean isEmpty(Object[] objs) {
        return objs == null || objs.length == 0;
    }
}
